var fs = require("fs");
var path = require("path");
var config = require('../../config.json');
var local_repos = config.local_repo_addr;
var LocalRepoProxy = require('../../proxy/local_repo.js');
var logger = require('log4js').getLogger();
var messageBroker = require('../../messageBroker.js');
var uuid = require('uuid');
var zip = require('../../common/zip.js');
var EventProxy = require('eventproxy');
var decompress = require('decompress');
var decompressUnzip = require('decompress-unzip');
var taskProxy = require('../../proxy/task.js');
var removeDir = require('../../common/removeDir.js');
var request = require('request');
var sdp_server = config.sdp_server.host + ":" + config.sdp_server.port;
var messageAgent = require('../../common/message.js');
/**
 * 下载商品
 * POST请求
 * @param：{fileName:"",version:"",other_info:"",type:""}
 */

exports.download = function (req, res, next) {
    var task_id = uuid.v1();
    res.format_json({task_id: task_id}, 200);

    request.post(sdp_server +　'/api/local_repo/download',{form:req.body},function(err,response,data){
        var dataJson = JSON.parse(data);
        if(dataJson.code == 200){
            if(dataJson.data.task_id){
                messageAgent.getMessage(sdp_server + '/api/message/' + dataJson.data.task_id, function(msg){
                    if(msg.err){
                        logger.error(msg.err);
                        messageBroker.emit(task_id, {task_id: task_id, body: req.body, err: "下载文件失败"});
                        return false;
                    }else {
                        if(msg.isCompleted){
                            messageBroker.emit(task_id, {task_id: task_id, body: req.body, err: null});
                            messageBroker.emit('dependency_download', {body: req.body, err: null});
                            logger.info("下载文件成功")
                        }
                    }
                })
            }
        }
    });
};

/**
 * 更新本地仓库文件和数据库版本信息
 * PUT请求
 * @param：{fileName:"",version:""}
 */
//todo :: 拆分调整 这里只更新local数据 不真正更新
exports.update = function (req, res, next) {
    var task_id = uuid.v1();
    res.format_json({task_id: task_id}, 200);
    setTimeout(function () {  //不靠谱
        request(
            {
                url: sdp_server +　'/api/local_repo/update',
                method: "PUT",
                json: true,
                body: req.body
            },
            function(err,response,data){
            if(data.code == 200){
                if(data.data.task_id){
                    messageAgent.getMessage(sdp_server + '/api/message/' + data.data.task_id, function(msg){
                        if(msg.err){
                            logger.error(msg.err);
                            messageBroker.emit(task_id, {task_id: task_id, body: req.body, err: "依赖更新失败"});
                            return false;
                        }else {
                            if(msg.isCompleted){
                                messageBroker.emit(task_id, {task_id: task_id, body: req.body, err: null});
                                messageBroker.emit('dependency_update', {body: req.body, err: null});
                                logger.info("依赖更新成功")
                            }
                        }
                    })
                }
            }
        });
    }, 200);
};


/**
 *执行callback
 */
function exe_callback(err, data, callback) {
    if (callback && typeof callback === 'function') {
        callback(err, data);
    }
}
